//
//  CSBMesgEnterSweeping.swift
//  CurSodoboAer
//
//  Created by CurSodoboAer on 2024/11/7.
//

import UIKit
import JDStatusBarNotification

class CSBMesgEnterSweeping: UIViewController {
    var csbColoter:UIColor = .brown
    var csbFplowe:CGFloat = 23.1
    var csbHeihei:UIFont?
    
    
    private let csbBackComButton = UIButton.init()
    private let uderNamerCSB = UILabel()
    
    private let csbAlertComButton = UIButton.init()
    let bottomCommentView = UIView.init()
    private let commrttextContent = UITextField.init()
    private let commentCSBButton = UIButton.init()
    
    private let aiChatCSBDAtaSongView = UITableView.init(frame: .zero, style: .plain)
    let videofCSBButton = UIButton.init()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        aiChatCSBDAtaSongView.isHidden = true
        aiChatCSBDAtaSongView.rowHeight = UITableView.automaticDimension
        aiChatCSBDAtaSongView.contentInset = UIEdgeInsets.zero
        
        aiChatCSBDAtaSongView.estimatedRowHeight = 70
        
        aiChatCSBDAtaSongView.register(CSBMesgrChatCell.self, forCellReuseIdentifier: "CSBMesgrChatCellID")
        aiChatCSBDAtaSongView.separatorStyle = .none
        aiChatCSBDAtaSongView.allowsSelection = false
        aiChatCSBDAtaSongView.showsVerticalScrollIndicator = false
        aiChatCSBDAtaSongView.backgroundColor = .clear
        aiChatCSBDAtaSongView.dataSource = self
        aiChatCSBDAtaSongView.delegate = self
    
        if csbColoter == UIColor.black {
            csbHeihei = UIFont.systemFont(ofSize: csbFplowe)
            self.view.backgroundColor = csbColoter
        }else{
            csbHeihei = UIFont.systemFont(ofSize: csbFplowe + 1)
        }
        guard csbHeihei != nil else { return  }

        if self.view.backgroundColor == csbColoter {
            let qurk = UIButton(configuration: UIButton.Configuration.plain())
            qurk.isHidden = true
            qurk.isEnabled = false
            view.addSubview(qurk)
        }
        
        commrttextContent.layer.cornerRadius = 22
        commrttextContent.layer.masksToBounds = true
        commrttextContent.placeholder = "Type a message…"
        commrttextContent.textColor = .black
        commrttextContent.backgroundColor = UIColor(red: 0.07, green: 0.07, blue: 0.07, alpha: 0.1200)
        commrttextContent.leftViewMode = .always
        commrttextContent.leftView = UIView(frame: CGRect.init(x: 0, y: 0, width: 30, height: 44))
       
        
        NotificationCenter.default.addObserver(self, selector: #selector(csbButtonClick), name: NSNotification.Name.init("laheiCSBkUsers"), object: nil)
        view.backgroundColor = UIColor(red: 0.64, green: 0.41, blue: 0.98, alpha: 1)
        csbBackComButton.setImage(UIImage.init(named: "csb_back"), for: .normal)
        csbBackComButton.addTarget(self, action: #selector(csbButtonClick), for: .touchUpInside)
        csbAlertComButton.setImage(UIImage.init(named: "active_report"), for: .normal)
        csbAlertComButton.addTarget(self, action: #selector(csbrepotrtClick), for: .touchUpInside)
        
        
        uderNamerCSB.textColor = .white
        uderNamerCSB.textAlignment = .left
        uderNamerCSB.font = UIFont(name: "Helvetica Bold", size: 16)
        uderNamerCSB.text = self._mesgDataCSB.whoToMeCSBDic["csbpersonName"] ?? ""
        commentCSBButton.setImage(UIImage.init(named: "tapsendCSB"), for: .normal)
           
        
        videofCSBButton.setImage(UIImage.init(named: "Csbvifeo"), for: .normal)
        videofCSBButton.addTarget(self, action: #selector(csbButtonClick), for: .touchUpInside)
        
        
        view.addSubview(csbBackComButton)
        view.addSubview(csbAlertComButton)
        view.addSubview(videofCSBButton)
        view.addSubview(uderNamerCSB)
        view.addSubview(aiChatCSBDAtaSongView)
        
       
        view.addSubview(bottomCommentView)
        bottomCommentView.addSubview(self.commrttextContent)
      
        bottomCommentView.addSubview(self.commentCSBButton)
        
        
        
        
        bottomCommentView.backgroundColor = UIColor(red: 0.75, green: 0.99, blue: 0.01, alpha: 1)
        
       
        
        let stattusBarHeight = UIApplication.shared.statusBarFrame.height
        csbBackComButton.snp.makeConstraints { make in
            make.leading.equalToSuperview().inset(12.xQQ)
            make.width.equalTo(24)
            make.height.equalTo(22)
            make.top.equalToSuperview().offset(stattusBarHeight + 15)
        }
        
        csbAlertComButton.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.trailing.equalToSuperview().inset(12.xQQ)
            make.centerY.equalTo(csbBackComButton)
        }
        
        videofCSBButton.snp.makeConstraints { make in
            make.width.equalTo(52.xQQ)
            make.height.equalTo(28.xQQ)
            make.right.equalTo(csbAlertComButton.snp.left).offset(-20)
            make.centerY.equalTo(csbAlertComButton)
        }
        
        uderNamerCSB.snp.makeConstraints { make in
            make.centerY.equalTo(csbBackComButton)
            make.leading.equalTo(csbBackComButton.snp.trailing).offset(13.xQQ)
           
        }
        
       
        
        aiChatCSBDAtaSongView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(csbBackComButton.snp.bottom).offset(30)
            make.bottom.equalToSuperview().offset(-86)
        }
        
        bottomCommentView.snp.makeConstraints { make in
            make.leading.trailing.bottom.equalToSuperview()
            make.height.equalTo(86)
        }
        
        commentCSBButton.snp.makeConstraints { make in
            make.width.equalTo(60.xQQ)
            make.height.equalTo(44.xQQ)
            make.top.equalToSuperview().offset(6)
            make.right.equalToSuperview().offset(-12.xQQ)
        }
        
        commrttextContent.snp.makeConstraints { make in
            make.leading.equalToSuperview().inset(12.xQQ)
            make.centerY.equalTo(commentCSBButton)
            make.trailing.equalTo(commentCSBButton.snp.leading).offset(-12)
            make.height.equalTo(44)
        }
        
        
        commentCSBButton.addTarget(self, action: #selector(sendMesgCSBClick), for: .touchUpInside)
        
        NotificationPresenter.shared.present("")
        NotificationPresenter.shared.displayActivityIndicator(true)
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.0, execute: DispatchWorkItem(block: {
            NotificationPresenter.shared.dismiss()
            self.aiChatCSBDAtaSongView.isHidden = false
            
        }))
    }
    
    private let csbBottonJoinComButton = UIButton.init()
    var _mesgDataCSB:CSBChatPack
    init(qugaoshCSB:[Int]?,mesgDataCSB:CSBChatPack) {
        if qugaoshCSB?.first ?? 0 > 0 {
            self._mesgDataCSB = mesgDataCSB
        }else{
            self._mesgDataCSB = mesgDataCSB
        }

        
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        bottomCommentView.layer.cornerRadius = 20
        bottomCommentView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
    }

    
    
    @objc func csbButtonClick(_ sender: UIButton) {
        if sender == self.csbBackComButton {
            self.navigationController?.popViewController(animated: true)
        }
        
        if sender == self.videofCSBButton {
            self.navigationController?.pushViewController(CSBCaovidroSweeping.init(qugaoshCSB: nil, activeDataCSB: self._mesgDataCSB.whoToMeCSBDic), animated: true)
        }
        
    }
    
    @objc func csbrepotrtClick()  {
        var csbBayui = [Int]()
        let csbNUmber = Array(repeating: 10, count: 64)
        let csbNumbe2 = Array(repeating: 96, count: 50)
        var cabSunmAll: Int = 0

        csbBayui.append(11)
        cabSunmAll = csbNUmber.count
        if cabSunmAll > 5 {
            csbBayui.append(contentsOf: csbNUmber)
        }else{
            csbBayui.append(contentsOf: csbNumbe2)
        }

        csbBayui.append(contentsOf: [52, 6, 11])
        csbBayui.removeLast()
        cabSunmAll = csbBayui.count

       
        let comenrAlrtPick = CSChoiUserOperaSweeping.init()
        if (cabSunmAll >= 0) && (csbBayui.count >= 0) {
            comenrAlrtPick.userDataTranCSB = self._mesgDataCSB.whoToMeCSBDic
            let navo =  CSBONabviSweeping.init(rootViewController:comenrAlrtPick )
            
            navo.modalPresentationStyle = .overCurrentContext
            self.present(navo, animated: true)
        }
        
    }
    

    @objc func sendMesgCSBClick(_ sender: UIButton) {
        var csbBayui = [Int]()
        let csbNUmber = Array(repeating: 10, count: 64)
        let csbNumbe2 = Array(repeating: 96, count: 50)
        var cabSunmAll: Int = 0

        csbBayui.append(11)
        cabSunmAll = csbNUmber.count
        if cabSunmAll > 5 {
            csbBayui.append(contentsOf: csbNUmber)
        }else{
            csbBayui.append(contentsOf: csbNumbe2)
        }

       
        if let conted = commrttextContent.text, conted.count != 0 {
            
            let diologdata = (true,conted)
            _mesgDataCSB.chatCSBList.append(diologdata)
            
            for (i,item) in  CSBFetcherAll.Hammer.allMyChatListCSB.enumerated() {
                if item.whoToMeCSBDic == _mesgDataCSB.whoToMeCSBDic{
                    CSBFetcherAll.Hammer.allMyChatListCSB[i] = _mesgDataCSB
                }
            }
            csbBayui.append(contentsOf: [52, 6, 11])
            csbBayui.removeLast()
            cabSunmAll = csbBayui.count

            if (cabSunmAll >= 0) && (csbBayui.count >= 0) {
                commrttextContent.text = nil
                commrttextContent.resignFirstResponder()
                self.aiChatCSBDAtaSongView.reloadData()
            }
           
           

           
            
            
        }else{
            NotificationPresenter.shared.present("Empty message cannot be send.....", includedStyle:.warning)
        }
    }
}


extension CSBMesgEnterSweeping:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return _mesgDataCSB.chatCSBList.count
        }
        return _mesgDataCSB.chatCSBList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let csbCell = tableView.dequeueReusableCell(withIdentifier:"CSBMesgrChatCellID") as! CSBMesgrChatCell
        csbCell.backgroundColor = .clear
        let meige = self._mesgDataCSB.chatCSBList[indexPath.row]
        csbCell.mychatContetnCSBLab.text = meige.1
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return csbCell
        }
        csbCell.updateUIREmaklCSB(ismeCSB: meige.0)
        csbCell.headSCB.image = UIImage.init(named: self._mesgDataCSB.whoToMeCSBDic["csbpersonHead"] ?? "")
        return csbCell
    }
    
    
}
